package com.yswl.yeeduo.ui.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.base.BaseWebViewActivity;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.view.GuidePointView;

public class GuideActivity extends BaseActivity implements OnPageChangeListener, OnClickListener {
	private ViewPager mViewPager;
	private ArrayList<View> pages = new ArrayList<View>();
//	GuidePointView point;

	private ImageView agreeIvState;
	private TextView priv;
	private ImageView startIv;
	LinearLayout llBtn_state;
	boolean isAgree = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		mViewPager = (ViewPager) findViewById(R.id.welcome_viewpager);
		initPages();
		mViewPager.setAdapter(new GuidePageAdater());
		mViewPager.setOnPageChangeListener(this);

		// 点
//		point = (GuidePointView) findViewById(R.id.indicator);
//		point.initPoint(pages.size());
	}

	private void initPages() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View page1 = inflater.inflate(R.layout.guide, null);
		View lastPage = inflater.inflate(R.layout.lastpage, null);
		initLastPage(lastPage);
		pages.add(page1);
		pages.add(lastPage);
	}

	private void initLastPage(View view) {
		startIv = (ImageView) view.findViewById(R.id.iv_start);
		startIv.setOnClickListener(this);
		priv = (TextView) view.findViewById(R.id.tv_agreement);
		priv.setOnClickListener(this);
		llBtn_state = (LinearLayout) view.findViewById(R.id.llBtn_guider_agreement);
		llBtn_state.setOnClickListener(this);
		agreeIvState = (ImageView) view.findViewById(R.id.iv_agreement);
		agreeIvState.setTag(true);
	}

	/**
	 * 
	 * 顶部宣传图片的内容提供
	 * 
	 */
	private class GuidePageAdater extends PagerAdapter {

		@Override
		public int getCount() {
			return pages.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(pages.get(position));
			return pages.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

	}

	@Override
	public void onPageScrollStateChanged(int position) {
	}

	@Override
	public void onPageScrolled(int position, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
//		point.setCurrPoint(position);// 设置当前状态
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.llBtn_guider_agreement:
			setCurrState();
			break;
		case R.id.iv_start:
			if ((Boolean) agreeIvState.getTag()) {
				startActivity(new Intent(this, MainActivity.class));
				finish();
			}else{
				ToastUtil.showShortMsg("未能同意服务条款");
			}

			break;
		case R.id.tv_agreement:
			startActivity(BaseWebViewActivity.createIntent(this, Constant.ConfigNet.priv, "服务条款", false, false, WebViewActivity2.class));
			break;

		default:
			break;
		}

	}

	private void setCurrState() {
		if (isAgree) {
			agreeIvState.setImageResource(R.drawable.btn_agreement_agree);
			agreeIvState.setTag(true);
			isAgree = false;
		} else {
			agreeIvState.setImageResource(R.drawable.btn_agreement_disagree);
			agreeIvState.setTag(false);
			isAgree = true;
		}
	}
	
	
	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}
}
